package com.i2bt.adapter;

import com.i2bt.R;
import com.i2bt.app.transmission.model.TransmissionGroup;
import com.i2bt.app.transmission.model.Transmission_item;
import com.i2bt.util.FileSizeConvertUtil;
import com.i2bt.view.DownLoadNumberProgressBar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ListAdapter extends BaseExpandableListAdapter {

	private Context context;

	private LayoutInflater childInflater;
	private LayoutInflater groupInflater;
	private List<TransmissionGroup> group;

	public ListAdapter(Context context, List<TransmissionGroup> group) {
		this.context = context;
		this.group = group;
		childInflater = (LayoutInflater) this.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		groupInflater = (LayoutInflater) this.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return group.get(groupPosition).getChildren().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Holder holder;
		final Transmission_item item = group.get(groupPosition).getChildren()
				.get(childPosition);
		if (convertView == null) {
			convertView = childInflater.inflate(R.layout.item_download, null);
			holder = new Holder();

			holder.img = (ImageView) convertView
					.findViewById(R.id.download_img);
			holder.img.setImageDrawable(item.getDrawable(this.context));
			holder.nameText = (TextView) convertView
					.findViewById(R.id.download_filename);
			holder.nameText.setText(item.getFileName());
			if (item.isComplete()) {
				holder.sizeText = (TextView) convertView
						.findViewById(R.id.download_info);
				holder.sizeText.setText(FileSizeConvertUtil.autoConvert(item
						.getCompleteSize()) + "  " + item.getCompleteDate());
				holder.numberProgress = (DownLoadNumberProgressBar) convertView
						.findViewById(R.id.download_progress);
				holder.numberProgress.setVisibility(View.GONE);
				holder.ctrlBtu = (Button) convertView
						.findViewById(R.id.download_control);
				holder.ctrlBtu.setVisibility(View.GONE);
			} else {
				holder.sizeText = (TextView) convertView
						.findViewById(R.id.download_info);
				holder.sizeText.setText(FileSizeConvertUtil.autoConvert(item
						.getCompleteSize())
						+ "/"
						+ FileSizeConvertUtil.autoConvert(item.getFileSize()));
				holder.numberProgress = (DownLoadNumberProgressBar) convertView
						.findViewById(R.id.download_progress);
				holder.numberProgress.setProgress(item.getCompleteRate());
				holder.ctrlBtu = (Button) convertView
						.findViewById(R.id.download_control);
				item.setBtu(holder.ctrlBtu);
				if (item.isDownload()) {
					item.startDownload(this.context);
				} else {
					item.stopDownload(this.context);
				}
				item.getBtu().setFocusable(false);
			}
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
			holder.img = (ImageView) convertView
					.findViewById(R.id.download_img);
			holder.img.setImageDrawable(item.getDrawable(this.context));
			holder.nameText = (TextView) convertView
					.findViewById(R.id.download_filename);
			holder.nameText.setText(item.getFileName());
			if (item.isComplete()) {
				holder.sizeText = (TextView) convertView
						.findViewById(R.id.download_info);
				holder.sizeText.setText(FileSizeConvertUtil.autoConvert(item
						.getCompleteSize()) + "  " + item.getCompleteDate());
				holder.numberProgress = (DownLoadNumberProgressBar) convertView
						.findViewById(R.id.download_progress);
				holder.numberProgress.setVisibility(View.GONE);
				holder.ctrlBtu = (Button) convertView
						.findViewById(R.id.download_control);
				holder.ctrlBtu.setVisibility(View.GONE);
			} else {
				holder.sizeText = (TextView) convertView
						.findViewById(R.id.download_info);
				holder.sizeText.setText(FileSizeConvertUtil.autoConvert(item
						.getCompleteSize())
						+ "/"
						+ FileSizeConvertUtil.autoConvert(item.getFileSize()));
				holder.numberProgress = (DownLoadNumberProgressBar) convertView
						.findViewById(R.id.download_progress);
				holder.numberProgress.setProgress(item.getCompleteRate());

				holder.ctrlBtu = (Button) convertView
						.findViewById(R.id.download_control);
				item.setBtu(holder.ctrlBtu);
				if (item.isDownload()) {
					item.startDownload(this.context);
				} else {
					item.stopDownload(this.context);
				}
				item.getBtu().setFocusable(false);
			}
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return group.get(groupPosition).getChildren().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return group.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView holder = null;
		if (convertView == null) {
			convertView = groupInflater.inflate(R.layout.item_downloa_group,
					null);
			holder = (TextView) convertView.findViewById(R.id.download_title);
			holder.setText(group.get(groupPosition).getTitle());
			convertView.setTag(holder);
		} else {
			holder = (TextView) convertView.getTag();
			holder.setText(group.get(groupPosition).getTitle());
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public class Holder {
		public ImageView img;
		public TextView nameText, sizeText;
		public DownLoadNumberProgressBar numberProgress;
		public Button ctrlBtu;
	}
}
